<template>
    <div>
        <div class="search-con search-con-top">
            <div>
                标题：
                <Input v-model="searchData.title" style="width: 200px"></Input>

                <Button @click="search" type="primary" icon="ios-search" style="margin-left: 10px;">搜索</Button>
                <Button @click="reset" type="warning" icon="ios-refresh" style="margin-left: 10px;">刷新</Button>
                <Button slot="extra" @click="edit(false, false)" type="success" style="margin-left: 10px;">添加</Button>
            </div>
        </div>
        <br>
        <Table :columns="columns" :data="list" @on-sort-change="customSort" :highlight-row="true" max-height="800" border
            stripe size="small">
            <template slot-scope="{ row , index}" slot="imgSlot">
                <show-img :imgUrl="row.img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="contextSlot">
                <div style="height: 200px;width: 100px;"><span v-html="row.context"></span></div>
            </template>
            <template slot-scope="{ row , index}" slot="makeSlot">
                <Button type="success" @click="edit(row, true)" size="small">详情</Button>
                <Button type="info" @click="edit(row, false)" size="small" style="margin-left: 1px;">编辑</Button>
                <Button type="error" @click="del(row.id)" style="margin-left: 1px;" size="small">删除</Button>
            </template>
        </Table>
        <br>

        <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize">
        </Paginate>


        <Modal title="活动信息" width="80" v-model="showAdd" :width="50" :footer-hide="formItem.is_detail"
            :mask-closable="false">
            <Form ref="formValidate" :model="formItem" :label-width="80">
                <FormItem label="活动标题">
                    <Input v-model="formItem.title" placeholder=""></Input>
                </FormItem>
                <!-- <FormItem label="活动类型">
                    <radioGroup v-model="formItem.type">
                        <Radio label="1">活动</Radio>
                        <Radio label="2">试驾</Radio>
                    </radioGroup>
                </FormItem> -->
                <FormItem label="活动图片">
                    <MyUpload :maxNumber="1" @getImages="getImg" :defaultImags="[formItem.img]"></MyUpload>
                </FormItem>
                <FormItem label="活动内容">
                    <RichText ref="ric" @getEditorContent="getRichTextContent" :content="formItem.content"></RichText>
                </FormItem>
                <FormItem label="开始时间">
                    <Date-picker type="datetime" v-model="formItem.begin_time"></Date-picker>
                </FormItem>
                <FormItem label="结束时间">
                    <Date-picker type="datetime" v-model="formItem.end_time"></Date-picker>
                </FormItem>
                <FormItem label="活动城市">
                    <Select v-model="formItem.city_ids" filterable multiple>
                        <Option :value="val.id" v-for="val in cityList" :key="val.id">{{ val.name }}</Option>
                    </Select>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" size="large" long @click="editModalOk">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import Paginate from "_c/paginate";
import MyUpload from "_c/my-upload"
import RichText from "_c/rich-text"
import ShowImg from '_c/show_img'


export default {
    name: "active_list",
    components: {
        Paginate, RichText, MyUpload, ShowImg
    },
    data() {
        return {
            searchData: {
                type: 2,
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: '公告',
                    key: 'title',
                    sortable: 'custom',
                },
                {
                    title: '图片',
                    key: 'img',
                    slot: "imgSlot"
                },
                // {
                //     title: '类型',
                //     key: 'type',
                //     render: (h, param) => {
                //         let txt = '';
                //         let color = '';
                //         if (param.row.type == 1) {
                //             txt = '活动';
                //             color = 'red';
                //         } else {
                //             txt = '试驾';
                //             color = 'blue'
                //         }
                //         return h('div', [
                //             h('span', {
                //                 style: {
                //                     color: color
                //                 }
                //             }, txt),
                //         ]);
                //     },
                // },
                {
                    title: '开始时间',
                    key: 'begin_time',
                    sortable: 'custom',
                },
                {
                    title: '结束时间',
                    key: 'end_time',
                    sortable: 'custom',
                },
                {
                    title: '试驾城市',
                    key: 'city_name',
                    sortable: 'custom',
                },
                {
                    title: '创建时间',
                    key: 'create_time',
                    sortable: 'custom',
                }, {
                    title: '操作',
                    slot: 'makeSlot',
                },
            ],
            // 数据
            list: [],
            total: 0,
            formItem: {},
            showAdd: false,
            cityList: [],
        }
    },
    methods: {
        getImg(items) {
            items.forEach(item => {
                this.formItem.img = item.url;
            });
        },
        getRichTextContent(value) {
            this.formItem.content = value;
        },
        // 查询管理员信息
        getList() {
            let params = {
                ...this.searchData,
            };
            this.$axios('/admin/article/getList', params).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
                type: 2,
            };
            this.getList();
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        customSort(column, key, order) {
            this.searchData.order_by = column.key;
            this.searchData.sort = column.order;
            this.getList();
        },
        editModalOk() {
            this.$axios('/admin/article/save', this.formItem).then(res => {
                this.$Message.success('添加成功');
                this.showAdd = false;
                this.formItem = {};
                this.getList();
            });
        },
        edit(row, isDetail) {
            if (row) {
                this.formItem = { ...row };
                this.formItem.type += '';
            } else {
                this.formItem = {
                    id: 0,
                    type: "2",
                }
            }
            this.formItem.is_detail = isDetail;
            this.getOpenCityList();
            this.showAdd = true;
            this.$nextTick(() => {
                this.$refs.ric.setContent(this.formItem.content);
            });
        },
        getOpenCityList() {
            this.$axios("/admin/config/getCityList").then(res => {
                this.cityList = res;
            });
        },
        del(id) {
            this.$axios('/admin/article/del', { id: id }).then(res => {
                this.$Message.success('删除成功');
                this.getList();
            });
        },
        handleSuccess(res, file) {
            this.$set(this.formItem, 'img', res.data.src);
        },
        handleFormatError(file) {
            this.$Notice.warning({
                title: "文件格式错误",
                desc: "不支持该文件类型",
            });
        },
        onCreated(editor) {
            this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        },
    },
    created() {
        this.getList();
    }
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
